<?php
$captchaFolder  = 'backup/';
$fileTypes      = '*.sql';
$newTime = 0;
$newfilename = "";
$nextfileName="";
$nextTime=0;
foreach (glob($captchaFolder . $fileTypes) as $Filename) 
{
 
  // Read file creation time 
  $FileCreationTime = filectime($Filename);
  // Is the file older than the given time span?
  if ($newTime < $FileCreationTime)
  {
    $newTime=$FileCreationTime;
    $newfilename=$Filename;
  }
}
print "LATEST  file is $newfilename <br>";
foreach (glob($captchaFolder . $fileTypes) as $Filename)
 {

 // echo $newfilename;
  // compare New file with Old file
  if ($Filename != $newfilename)
  {
    $FileCreationTime = filectime($Filename);
    if ($nextTime < $FileCreationTime)
  {
    $nextTime=$FileCreationTime;
    $nextfileName=$Filename;
  }
  }
}
print " NEXT LATEST  file is $nextfileName";
 foreach (glob($captchaFolder . $fileTypes) as $Filename)
 {
  if ($Filename != $nextfileName && $Filename !=$newfilename)
 {
   unlink($Filename);
 }
}
?>